package com.goodfox.web.generic.authority.service.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.goodfox.web.generic.account.entity.Account;
import com.goodfox.web.generic.account.mapper.AccountMapper;
import com.goodfox.web.generic.role.mapper.RoleMapper;

/**
 * 重写spring security用户详细信息service
 * @author qin gt
 *
 */
@Transactional
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

	/** log4j日志器 */
	private Logger logger=Logger.getLogger(UserDetailsServiceImpl.class);

	/** 账户Mapper */
	@Autowired
	private AccountMapper accountMapper;

	/** 角色Mapper */
	@Autowired
	private RoleMapper roleMapper;
	

	/** 
	 * 覆盖原接口中加载用户类，主要用于获取登录到系统中的用户详细信息 
	 * @param username 登录名
	 * @return
	 */
	@Override
	public UserDetails loadUserByUsername(String username)throws UsernameNotFoundException, DataAccessException {
		logger.debug("登录名:"+username);
		Account account=null;
		try{
			account=accountMapper.selectAccountByName(username);
			if(account!=null){
				account.setRole(roleMapper.selectRoleById(account.getRoleId()));
			}
		}catch(Exception ex){
			logger.error("登陆异常：",ex);
		}
		return account;
	}
}
